﻿'*******************************************************************************************
'* Copyright Brügmann Software GmbH, Papenburg
'* Author: Björn Kremer
'* Contact: codeplex<at>patorg.de
'* 
'* VB.CMIS is a VB.NET implementation of the Content Management Interoperability Services (CMIS) standard
'*
'* This file is part of VB.CMIS.
'* 
'* VB.CMIS is free software: you can redistribute it and/or modify
'* it under the terms of the GNU Lesser General Public License as published by
'* the Free Software Foundation, either version 3 of the License, or
'* (at your option) any later version.
'* 
'* VB.CMIS is distributed in the hope that it will be useful,
'* but WITHOUT ANY WARRANTY; without even the implied warranty of
'* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
'* GNU Lesser General Public License for more details.
'* 
'* You should have received a copy of the GNU Lesser General Public License
'* along with VB.CMIS. If not, see <http://www.gnu.org/licenses/>.
'*******************************************************************************************
Imports System.ServiceModel.Syndication
Imports CMISObjectModel.TypeDefinitions
Imports System.Xml

Namespace Atom
   <Xml.Serialization.XmlRoot("entry", Namespace:=CmisNamespaceConstants.Atom)>
   Public Class CMISAtomEntryTypeDefinition
      Inherits CMISAtomEntry(Of TypeDefinition)
      Public Sub New()
         MyBase.New()
      End Sub
      Public Sub New([object] As TypeDefinition, atomId As String, atomTitle As String, atomCreationDate As DateTimeOffset, atomLastUpdatedTime As DateTimeOffset, atomAuthors As List(Of SyndicationPerson))
         MyBase.New([object], atomId, atomTitle, atomCreationDate, atomLastUpdatedTime, atomAuthors)
      End Sub
      Public Overrides Sub ReadXml(reader As Xml.XmlReader)
         '  MyBase.ReadXml(reader)
         Dim doc As New System.Xml.XmlDocument()
         doc.Load(reader)


         Dim node As System.Xml.XmlNode = Common.GetItemCaseInsensitiveFromXML(doc, "entry")
         Dim typeNode As Xml.XmlNode = Nothing
         For Each child As System.Xml.XmlElement In node.ChildNodes
            Dim lName As String = Common.GetNodeNameWithoutPrefix(child).ToLowerInvariant
            If lName.Equals("type") Then
               typeNode = child
               Exit For
            End If
         Next
         If typeNode Is Nothing Then
            Exit Sub 'TODO
         End If
         For Each child As XmlNode In typeNode.Attributes
            Dim lName As String = CMISObjectModel.Common.GetNodeNameWithoutPrefix(child).ToLowerInvariant
            If lName.Equals("type") Then
               Dim lText As String = child.InnerText.ToLowerInvariant
               If lText.Equals(TypeDefinitions.TypeDocumentDefinition.SharedCMISTypeName.ToLowerInvariant()) Then
                  Me.Object = Parse(Of TypeDefinitions.TypeDocumentDefinition)(typeNode)
                  Exit For
               ElseIf lText.Equals(TypeDefinitions.TypeFolderDefinition.SharedCMISTypeName.ToLowerInvariant()) Then
                  Me.Object = Parse(Of TypeDefinitions.TypeFolderDefinition)(typeNode)
                  Exit For
               ElseIf lText.Equals(TypeDefinitions.TypePolicyDefinition.SharedCMISTypeName.ToLowerInvariant()) Then
                  Me.Object = Parse(Of TypeDefinitions.TypePolicyDefinition)(typeNode)
                  Exit For
               ElseIf lText.Equals(TypeDefinitions.TypeRelationshipDefinition.SharedCMISTypeName.ToLowerInvariant()) Then
                  Me.Object = Parse(Of TypeDefinitions.TypeRelationshipDefinition)(typeNode)
                  Exit For
               End If
            End If
         Next
      End Sub
      Private Shared Function Parse(Of T)(node As Xml.XmlNode) As T
         Using stream As New IO.StringReader(node.OuterXml)
            Dim serializer As New Serialization.XmlSerializer(GetType(T))
            Return DirectCast(serializer.Deserialize(stream), T)
         End Using
      End Function
   End Class
End Namespace